211. k8s service endpoint無法連結
前言
某天在建立新的叢集,
服務都已經架好準備給RD了,
最後才發現,平常用的連線方式連不到GCP的Memorystore.Redis。
這個問題卡了整整一週,
不斷的刪除重建、比對、找差異。
故事展開
GKE 版本: 1.28.7-gke.1026000
先簡單說一下架構,
istio增加6379 的port ,
新增redis的svc以及endpoint,
由於redis是GCP的服務,故本身沒有pod,
讓istio能夠透過virtusalService連到這個service,
然後RD透過這個ip以及port連到 GCP的Redis。
先來看錯誤的使用方式,

在port name上面設定redis的名稱,
導致svc的endpoints上面無法綁定。
不設定port name的話

則可以正常綁定。
目前猜測,可能跟 IANA的port name有關係。
但為什麼會這樣,
就不清楚了。
# Service Name and Transport Protocol Port Number Registry
目前測試出來,只要有設定Name就會綁不到endpoints。
另外,EndpointSlice目前(2024/05/20)仍會無法綁定,
請先改用Endpoint的api。

apiVersion: discovery.k8s.io/v1
kind: EndpointSlice
metadata:
  name: redis # must be the same as service name
  namespace: istio-system
  labels:
    # You should set the "kubernetes.io/service-name" label.
    # Set its value to match the name of the Service
    kubernetes.io/service-name: redis
addressType: IPv4
ports:
  - name: redis # should match with the name of the service port defined above
    appProtocol: redis
    protocol: TCP
    port: 6379
endpoints:
  - addresses:
      - "10.1.11.43"
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: istio-system
spec:
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379
      name: redis    
參照官方文件的作法也不會綁定。
Services without selectors
2024/05/8
有另一個工程師告知,
endpoint那邊的Port Name也加上去的話,
就好了。
結論
頂多改改 Endpoints的address就好了,
port name那些,別亂加。
要加就要一起加,且名稱也要一樣。
---
apiVersion: v1
kind: Endpoints
metadata:
  name: redis # must be the same as service name
  namespace: istio-system
subsets:
  - addresses:
      - ip: 10.122.11.43
    ports:
      - port: 6379
	    name: redis
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: istio-system
spec:
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379
      name: redis